﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Media;
using Plane.Properties;

namespace Plane
{
    public class MyBullet
    {
        private int x;//子弹横坐标
        private int y;//子弹纵坐标
        private const int BULLET_OFFSET = 18;//移动速度
        public int Angle;//子弹角度
        private Image bulImg;//定义子弹图片
        private const double PI = Math.PI;
        public static List<MyBullet> mybulList = new List<MyBullet>();//子弹对象集合

        static Bitmap bm = new Bitmap(Resources.bomb4);//爆炸图片
        public bool isHit = false;//碰撞标志

        private static (string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User;
        private string username;
        public MyBullet(string username)
        {
            this.username = username;
            User = UserDbHelper.GetUserInformationByUsername(username);
        }

         public MyBullet(int bx, int by, int angle)
        {
            x = bx;
            y = by;
            Angle = angle;
            switch (Angle)
            {
                case 0:
                    bulImg = Resources.bul02;
                    y -= 17;
                    break;
                case 30:
                    bulImg = Resources.bul02_30;
                    x += 12;
                    y -= 12;
                    break;
                case 60:
                    bulImg = Resources.bul02_60;
                    x += 2;
                    y -= 17;
                    break;
                case 120:
                    bulImg = Resources.bul02_120;
                    x -= 35;
                    y -= 12;
                    break;
                case 150:
                    bulImg = Resources.bul02_150;
                    x -= 20;
                    y -= 12;
                    break;
                default:
                    break;
            }
        }


        /// 通过按键盘J键来产生我方子弹 <summary>
        public static void ProduceMybul()
        {
            Enum.TryParse(User.KeyFire, ignoreCase: true, out Keys fireKey);
            if (!MyPlane.isGameOver && MyPlane.IsKeyDown(fireKey))
            {
                mybulList.Add(new MyBullet(MyPlane.x + 13, MyPlane.y - 10, 0));
                if (MyPlane.isGetGun == 1)
                {
                    mybulList.Add(new MyBullet(MyPlane.x + 13, MyPlane.y - 8, 60));
                    mybulList.Add(new MyBullet(MyPlane.x + 30, MyPlane.y - 12, 120));
                }
                else if (MyPlane.isGetGun >= 2)
                {
                    mybulList.Add(new MyBullet(MyPlane.x + 13, MyPlane.y - 8, 60));
                    mybulList.Add(new MyBullet(MyPlane.x + 7, MyPlane.y - 8, 30));
                    mybulList.Add(new MyBullet(MyPlane.x + 30, MyPlane.y - 12, 120));
                    mybulList.Add(new MyBullet(MyPlane.x, MyPlane.y - 7, 150));
                }
            }
        }

        
        /// 显示我方子弹
        
        public void ShowMybul(Graphics e)
        {
            e.DrawImage(bulImg, new Point(x, y));
        }
       

        /// 我方子弹移动
 
        public static void MoveMybul(Graphics g)
        {
            for(int i = 0;i < mybulList.Count; i++)
            {
                mybulList[i].ShowMybul(g);
                switch (mybulList[i].Angle)
                {
                    case 0:
                        mybulList[i].y -= BULLET_OFFSET;
                        break;
                    case 60:
                        mybulList[i].x += (int)(BULLET_OFFSET / 2);
                        mybulList[i].y -= (int)(BULLET_OFFSET * Math.Cos(PI / 6));
                        break;
                    case 30:
                        mybulList[i].x += (int)(BULLET_OFFSET * Math.Cos(PI / 6));
                        mybulList[i].y -= (int)(BULLET_OFFSET / 2);
                        break;
                    case 120:
                        mybulList[i].x -= (int)(BULLET_OFFSET / 2);
                        mybulList[i].y -= (int)(BULLET_OFFSET * Math.Cos(PI / 6));
                        break;
                    case 150:
                        mybulList[i].x -= (int)(BULLET_OFFSET * Math.Cos(PI / 6));
                        mybulList[i].y -= (int)(BULLET_OFFSET / 2);
                        break;
                }
                if (mybulList[i].y < 0 || mybulList[i].x > 415 || mybulList[i].x < 0)
                {
                    mybulList.Remove(mybulList[i]);
                }
            }
        }

        /// 敌机碰撞检测方法

        public static void IsHitEnemy(Graphics g)
        {
            Rectangle myPlaneRect = new Rectangle(MyPlane.x, MyPlane.y, MyPlane.myPlaneImg.Width, MyPlane.myPlaneImg.Height);

            for (int i = mybulList.Count - 1; i >= 0; i--)
            {
                int bul_x = mybulList[i].x < 0 ? 0 : mybulList[i].x;
                int bul_y = mybulList[i].y < 0 ? 0 : mybulList[i].y;
                Rectangle mybulRect = new Rectangle(bul_x, bul_y, 8, 10);

                for (int j = Fighter.fighters.Count - 1; j >= 0; j--)
                {
                    int fighter_x = Fighter.fighters[j]._x < 0 ? 0 : Fighter.fighters[j]._x;
                    int fighter_y = Fighter.fighters[j]._y < 0 ? 0 : Fighter.fighters[j]._y;
                    Rectangle fighterRect = new Rectangle(fighter_x, fighter_y, 65, 45);

                    if (mybulRect.IntersectsWith(fighterRect))
                    {
                        mybulList.RemoveAt(i);
                        Fighter.fighters[j].flag = true;

                        if (MyPlane.score < 100)
                        {
                            MyPlane.score += 1;
                        }

                        break; // 子弹已命中，退出当前敌机循环
                    }

                    if (myPlaneRect.IntersectsWith(fighterRect))
                    {
                        Fighter.fighters[j].flag = true;

                        if (MyPlane.score < 100)
                        {
                            MyPlane.score += 1;
                        }
                    }
                }
            }
        }
        public static void reset()
        {
            mybulList.Clear();
        }

    }
}